<?php
class stFlickrToolboxclass{
	
        public function __construct($config, $option){
            $this->config = $config;
            $this->option = $option;
            
            $this->key = $option['flickr_key'];
            $this->secret = $option['flickr_secret'];
            require_once $this->config['pluginDir'].'libs/flickr/phpFlickr.php';

            $this->flickr = new phpFlickr($this->key, $this->secret);
            $this->flickr->setToken($option['flickr_token']);
	}

        public function loadmodel($modelName) {
                $className = $modelName . "_model";
                $fileName = $this->config ['pluginDir'] . "models/{$modelName}.php";
                require_once $fileName;
                $obj = new $className($this->config);
                return $obj;
        }

        public function auth() {
//            session_unregister('phpFlickr_auth_token');
//            session_unregister('api_key');
            return $this->flickr->auth('delete');
        }

        public function getAccessToken($frob) {
            if (empty($frob)) {
                return $this->flickr->auth('delete', false);
            } else {
                return $this->flickr->auth_getToken($frob);
            }
        }

        public function syncPhoto($page = 1){
            $stFlickrToolbox_model = $this->loadmodel ( "stflickr" );
            $args = array(
                'page'  => $page,
            );
            $photos = $this->flickr->people_getPhotos($this->option['flickr_user_id'], $args);

            $page = $photos['photos']['page'];
            $pages = $photos['photos']['pages'];
            $user_id = get_current_user_id();
            if($page < $pages){                
                $page += 1;
                $stFlickrToolbox_model->savestFlickrToolbox($photos['photos']['photo'], $user_id);
                $this->syncPhoto($page);
            }else{
                $stFlickrToolbox_model->savestFlickrToolbox($photos['photos']['photo'], $user_id);
                $this->syncSet();
            }
        }

        public function syncSet(){
            $stFlickrToolbox_set_model = $this->loadmodel ( "stflickr_set" );
            $stFlickrToolbox_set_photo = $this->loadmodel ( "stflickr_set_photo" );
            $stFlickrToolbox_set_photo->emptyTable();
            $sets = $this->flickr->photosets_getList($this->option['flickr_user_id']);
            $user_id = get_current_user_id();
            $stFlickrToolbox_set_model->savestFlickrToolboxSet($sets['photoset'], $user_id);
            if(count($sets['photoset']) > 0){
                foreach ($sets['photoset'] as $set) {
                    $this->saveSetPhoto($set['id']);
                }
            }
        }

        public function saveSetPhoto($set_id, $page = 1){
            $stFlickrToolbox_set_photo = $this->loadmodel ( "stflickr_set_photo" );
            $args = array(
                'page'  => $page,
            );
            $photos = $this->flickr->photosets_getPhotos($set_id, $args);
            //var_dump($photos['photoset']['photo']);
            $page = $photos['photoset']['page'];
            $pages = $photos['photoset']['pages'];
            if($page < $pages){
                $page += 1;
                $stFlickrToolbox_set_photo->saveStflickrSetPhoto($set_id, $photos['photoset']['photo']);
                $this->saveSetPhoto($set_id, $page);
            }else{
                $stFlickrToolbox_set_photo->saveStflickrSetPhoto($set_id, $photos['photoset']['photo']);
            }
        }

        public function getPhotoInfo($photo_id){
            return $this->flickr->photos_getInfo($photo_id);
        }
}
?>